import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:tiktokadult/hj_page/community_business/hot_up_user/hot_up_user_view.dart';
import 'package:tiktokadult/hj_page/community_business/widget/community_post_widget.dart';
import 'package:tiktokadult/hj_page/community_business/widget/community_section_cell.dart';
import 'package:tiktokadult/hj_page/community_business/widget/community_sort_header.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';
import 'package:tiktokadult/tools_base/banner/ads_grid_view_widget.dart';
import 'package:tiktokadult/tools_base/loading/loading_center_widget.dart';
import 'package:tiktokadult/tools_base/refresh/pull_refresh.dart';

import '../../../hj_model/home/home_tabs_entity.dart';
import '../../../hj_model/video_model.dart';
import '../../../hj_utils/sliver_delegate.dart';
import '../../../tools_base/loading/l_error_widget.dart';
import 'community_tab_logic.dart';

class CommunityTabView extends StatelessWidget {
  final ModuleData model;
  final int index;
  final Function()? cancelCallBack;

  const CommunityTabView({
    super.key,
    required this.model,
    this.index = 0,
    this.cancelCallBack,
  });

  @override
  Widget build(BuildContext context) {
    return GetBuilder<CommunityTabLogic>(
      init: CommunityTabLogic(model, index: index),
      global: false,
      builder: (logic) {
        return Container(
          child: pullYsRefresh(
            onInit: (ctr) => logic.refreshController = ctr,
            onRefresh: (_) => logic.fetchPagedata(),
            onLoading: (_) => logic.fetchPagedata(isRefresh: false),
            child: CustomScrollView(
              controller: logic.sctrl,
              slivers: [
                SliverToBoxAdapter(
                  child: AdsGridViewWidget(
                    7,
                    isBigStyle: true,
                    padding: EdgeInsets.fromLTRB(10.w, 10.h, 10.w, 10.h),
                  ),
                ),
                SliverToBoxAdapter(
                  child: CommunitySpecialView(logic.specials),
                ),
                SliverPersistentHeader(
                  // key: ValueKey(logic.curMenuIndex),
                  pinned: true,
                  floating: true,
                  delegate: MySliverDelegate(
                    maxHeight: 40,
                    minHeight: 40,
                    callTop: (shrinkOffset) {
                      if (shrinkOffset > 0) {
                        WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
                          logic.sortMenuIsTop.value = true;
                        });
                      }
                      if (shrinkOffset <= 0) {
                        WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
                          logic.sortMenuIsTop.value = false;
                        });
                      }
                    },
                    child: Obx(
                      () => CommunitySortHeader(
                        isInTop: logic.sortMenuIsTop.value,
                        sortOnTap: (sort) {
                          logic.sort = sort;
                          logic.fetchPagedata(needLoading: true);
                        },
                      ),
                    ),
                  ),
                ),
                _buildTableView(logic),
              ],
            ),
          ),
        );
      },
    );
  }

  Widget _buildTableView(CommunityTabLogic logic) {
    if (logic.isLoading)
      return SliverToBoxAdapter(
        child: SizedBox(
          height: 300,
          child: LoadingCenterWidget(),
        ),
      );
    if (logic.dataSource.isEmpty)
      return SliverToBoxAdapter(
        child: SizedBox(
          height: 300,
          child: CErrorWidget(
            retryOnTap: () => logic.fetchPagedata(),
          ),
        ),
      );

    return SliverPadding(
      padding: EdgeInsets.only(top: 10),
      sliver: SliverList.separated(
        separatorBuilder: (_, __) => 0.h.sizeBoxH,
        itemBuilder: (BuildContext context, int index) {
          VideoModel videoModel = logic.dataSource[index];
          if (videoModel.isHotUp()) {
            return HotUpUserView(hotUpList: videoModel.hotUpList);
          } else {
            return CommunityPostWidget(videoModel: videoModel, videoModels: logic.dataSource);
          }
        },
        itemCount: logic.dataSource.length,
      ),
    );
  }
}
